<?php

require_once "Basic.php";

class Veiculo {
  
  public static function getAll($filial, $db = null) {
    $wasNull = false;
    if($db === null) {
      $wasNull = true;
      $db = new DBUtils();
      $db->connect();
    }
    $db->runQuery("select * from FrotaDisponivel(?)", array($filial));
    $all = array();
    for($item = $db->getItem(); $item !== null; $item = $db->getItem()) {
      $all[$item->idVeiculo] = $item;
    }
    if($wasNull) {
      $db->close();
    }
    return $all;
  }
  
  public static function getAllForChoice($filial, $db = null) {
    $all = Veiculo::getAll($filial, $db);
    foreach($all as $k => &$v) {
      $v = $v->placa;
    }
    return $all;
  }
  
  public static function getAllForReservation($idReserva, $db = null) {
    $wasNull = false;
    if($db === null) {
      $wasNull = true;
      $db = new DBUtils();
      $db->connect();
    }
    $db->runQuery("select * from FrotaDisponivelReserva(?)", array($idReserva));
    $all = array();
    for($item = $db->getItem(); $item !== null; $item = $db->getItem()) {
      $all[$item->idVeiculo] = $item;
    }
    if($wasNull) {
      $db->close();
    }
    return $all;
  }
  
  public static function getAllForReservationForChoice($idReserva, $db = null) {
    $all = Veiculo::getAllForReservation($idReserva, $db);
    foreach($all as $k => &$v) {
      $v = $v->placa;
    }
    return $all;
  }
  
}
